﻿using ACC.Business;
using ACC.Business.Process;
using ACC.Business.Stn;
using ACC.Business.Utils;
using ACC.Data.ACCLog;
using ACC.Data.Comm;
using ACC.Data.Entities.Mes;
using ACC.Data.Line;
using ACC.Device;
using ACC.LineDevice.KukaPack;
using System;
using System.Data;
using System.Text;

namespace Script
{
    /// <summary>
    /// 人工拆包 OP L1M00010, STN L1M00011
    /// </summary>
    public class L1M00010 : StnProcessKukaPack
    {
        public L1M00010(StnRunningData data)
            : base(data)
        {
            IsReport = false;
            IsACCReport = false;
            IsPalletRelationSN = false;
            IsNormalMaterial = false;
            materialTypeCount = -1; //防止底层进行扫料处理
        }

        protected override void CreateBusinessLogic()
        {
             base.CreateBusinessLogic();
             _loadProcess = new LoadProcess_L1M00010Ex(_data);
             _unloadProcess = new UnloadProcess_L1M00010Ex(_data);
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 0;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 66;
                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 34;
                kukaSiemensDev.DB193_ACC_Offset = 68;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 34;
                kukaSiemensDev.DB194_PLC_Data_Offset = 270;
            }

            return dev;

        }

        public override ACCCode ReadLoadParam(ACCDevice dev, out ACCDevice.LoadParam param)
        {
            ACCCode rs = base.ReadLoadParam(dev, out param);
            if (rs == ACCCode.ACCOK)
            {
                string partNo = string.Empty;
                //string currentWoNo = _womsProcess.CurrentWoNo();
                var woCurrent = _womsProcess.GetCurrentWorkOrder();
                string currentWoNo = woCurrent == null || woCurrent.IsNull ? string.Empty : woCurrent.Header.WONo;
                string orderNo = woCurrent == null || woCurrent.IsNull ? string.Empty : woCurrent.Header.PONo;

                // 如果工单号为空、说明还没有进行生产
                //if (string.IsNullOrEmpty(currentWoNo))
                {
                    // 从下达的工单中取得一个工单
                    // 取得方法:已下达的工单（STATUS = 1）按照【SORT】字段升序排序后的第一个工单
                    int plannedQty;
                    string wono = GetWorkOrder(_lineData.Line, string.Empty, out partNo, out orderNo, out plannedQty);
                    if (!string.IsNullOrEmpty(wono) && currentWoNo != wono)
                    {
                        // 更新当前工单
                        SetCurrentWorkOrder(wono, partNo);
                    }
                    else if (string.IsNullOrEmpty(wono))
                    {
                        // 没有工单需要生产
                        return rs = ACCCode.UserDef_100;
                    }
                    currentWoNo = wono;
                }

                rs = _womsProcess.CheckWorkOrder(_data.IsFirstStn);
                if (rs == ACCCode.UserDef_104) // 工单已满
                {
                    // 这里需要获取一个新的未用的工单（和当前不一样的工单，因为当前的可能还未结束）
                    string wono = GetNewWorkOrder(_lineData.Line, string.Empty, currentWoNo, out partNo, out orderNo);

                    if (!string.IsNullOrEmpty(wono) && currentWoNo != wono)
                    {
                        // 更新当前工单
                        SetCurrentWorkOrder(wono, partNo);
                    }
                    else if (string.IsNullOrEmpty(wono))
                    {
                        // 没有工单需要生产
                        return ACCCode.UserDef_100;

                    }
                    currentWoNo = wono;
                    rs = _womsProcess.CheckWorkOrder(_data.IsFirstStn);
                    if (rs != ACCCode.ACCOK)
                        return rs;
                }
                rs = AutoChangePartNo(partNo, ref param);

                string orderLot = GetOrderCellLot(orderNo);
                if (!CheckCellLot(orderLot, param.UnitSN))
                {
                    rs = ACCCode.UserDef_113;       // 电芯批次号不一致
                }
            }

            return rs;
        }

        /// <summary>
        /// 校验当前到人工拆包工位的电芯批次是否和订单中的电芯批次号一致
        /// </summary>
        /// <param name="orderLot">订单批次号</param>
        /// <param name="currentLot">人工扫描PLC传入的当前电芯批次号</param>
        /// <returns></returns>
        public bool CheckCellLot(string orderLot, string currentLot)
        {
            if (!string.IsNullOrEmpty(orderLot) && !string.IsNullOrEmpty(currentLot))
            {
                foreach (string lot in orderLot.Split(';'))
                {
                    if (currentLot == lot)
                        return true;
                }
            }
            return false;
        }

        public override ACCCode ReadUnloadParam(ACCDevice dev, out ACCDevice.UnloadParam param)
        {
            ACCCode rs = base.ReadUnloadParam(dev, out param);
            if (rs == ACCCode.ACCOK)
            {
                param.IsOk = true;
            }

            return rs;
        }
    }

    public class LoadProcess_L1M00010Ex : LoadProcess
    {
        public LoadProcess_L1M00010Ex(StnRunningData data)
            : base(data)
        {

        }
        /// <summary>
        ///不需要进行load业务的处理
        /// </summary>
        /// <param name="dev"></param>
        public override void ProcessLoad(ACCDevice dev)
        {

        }
    }

    public class UnloadProcess_L1M00010Ex : UnloadProcess
    {
        public UnloadProcess_L1M00010Ex(StnRunningData data)
            : base(data)
        {
        }

        public override void ProcessUnload(ACC.Device.ACCDevice dev)
        {

        }
    }
}
